package com.jdew.thkj.fragment;

import android.app.Activity;
import android.support.design.widget.TabLayout;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.jdew.thkj.R;
import com.jdew.thkj.activity.LoginActivity;
import com.jdew.thkj.activity.UploadMaterialActivity;
import com.jdew.thkj.base.BaseFragment;
import com.jdew.thkj.view.Reflex;

import butterknife.Bind;
import butterknife.ButterKnife;

public class HuiFenCicleFragment extends BaseFragment implements View.OnClickListener {
    @Bind(R.id.tablayout)
    TabLayout tablayout;
    @Bind(R.id.viewpager)
    ViewPager viewpager;
    @Bind(R.id.title)
    TextView title;
    @Bind(R.id.right)
    ImageView right;
    @Bind(R.id.back)
    ImageView back;

    @Override
    protected int getContentViewId() {
        return R.layout.fragment_huifen;
    }

    @Override
    protected void onCreateViewT() {
    }

    @Override
    public void initData() {
        title.setText("惠粉圈");
        sp = getActivity().getSharedPreferences("user", Activity.MODE_PRIVATE);
        editor = sp.edit();
        right.setOnClickListener(this);
        ShortPagerAdapter adapter = new ShortPagerAdapter(getChildFragmentManager());
        viewpager.setAdapter(adapter);
        tablayout.setupWithViewPager(viewpager);
        back.setVisibility(View.GONE);
        Reflex.setTabWidth(tablayout, 60);
    }

    public static HuiFenCicleFragment newInstance() {
        HuiFenCicleFragment fragment = new HuiFenCicleFragment();
        return fragment;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.right: {
                if (null == sp.getString("SpareTwo", "") || sp.getString("SpareTwo", "").equals("")) {
                    startActivity(LoginActivity.class);
                    return;
                }
                startActivity(UploadMaterialActivity.class);
            }
            break;
        }
    }

    private class ShortPagerAdapter extends FragmentPagerAdapter {
        public String[] mTilte = null;

        public ShortPagerAdapter(FragmentManager fm) {
            super(fm);
            mTilte = getResources().getStringArray(R.array.huifen_cicle);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mTilte[position];
        }

        @Override
        public BaseFragment getItem(int position) {
            BaseFragment fragment = FragmentFactory.createFragment7(position);
            return fragment;
        }

        @Override
        public int getCount() {
            return mTilte.length;
        }
    }
}
